<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高级模糊搜索功能测试</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ccc; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        button { margin: 5px; padding: 10px; }
        input { padding: 8px; margin: 5px; width: 200px; }
        .product-item { border: 1px solid #ddd; margin: 10px 0; padding: 10px; }
        .search-results { max-height: 400px; overflow-y: auto; }
        .test-buttons { display: flex; flex-wrap: wrap; gap: 10px; }
        .test-btn { padding: 8px 12px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; }
        .test-btn:hover { background: #0056b3; }
    </style>
</head>
<body>
    <h1>高级模糊搜索功能测试</h1>
    
    <div class="test-section">
        <h3>1. 搜索测试</h3>
        <input type="text" id="searchInput" placeholder="输入搜索关键词..." value="裤子">
        <button onclick="testSearch()">搜索</button>
        <div id="searchStatus"></div>
    </div>
    
    <div class="test-section">
        <h3>2. 快速测试按钮</h3>
        <div class="test-buttons">
            <button class="test-btn" onclick="quickTest('裤子')">测试"裤子"</button>
            <button class="test-btn" onclick="quickTest('鱼')">测试"鱼"</button>
            <button class="test-btn" onclick="quickTest('子')">测试"子"(噪音)</button>
            <button class="test-btn" onclick="quickTest('服装')">测试"服装"</button>
            <button class="test-btn" onclick="quickTest('手机')">测试"手机"</button>
            <button class="test-btn" onclick="quickTest('苹果')">测试"苹果"</button>
        </div>
    </div>
    
    <div class="test-section">
        <h3>3. 搜索结果</h3>
        <div id="searchResults" class="search-results"></div>
    </div>
    
    <div class="test-section">
        <h3>4. 控制台日志</h3>
        <div id="consoleLog"></div>
    </div>

    <!-- 引入必要的脚本 -->
    <script src="js/api.js"></script>
    
    <script>
        // 重写console.log来显示在页面上
        const originalLog = console.log;
        const originalWarn = console.warn;
        const originalError = console.error;
        
        function addToConsole(message, type = 'info') {
            const logDiv = document.getElementById('consoleLog');
            const timestamp = new Date().toLocaleTimeString();
            logDiv.innerHTML += `<div class="${type}">[${timestamp}] ${message}</div>`;
            logDiv.scrollTop = logDiv.scrollHeight;
        }
        
        console.log = function(...args) {
            originalLog.apply(console, args);
            addToConsole(args.join(' '), 'info');
        };
        
        console.warn = function(...args) {
            originalWarn.apply(console, args);
            addToConsole(args.join(' '), 'error');
        };
        
        console.error = function(...args) {
            originalError.apply(console, args);
            addToConsole(args.join(' '), 'error');
        };
        
        // 测试搜索功能
        async function testSearch() {
            const keyword = document.getElementById('searchInput').value.trim();
            await performSearch(keyword);
        }
        
        // 快速测试
        async function quickTest(keyword) {
            document.getElementById('searchInput').value = keyword;
            await performSearch(keyword);
        }
        
        // 执行搜索
        async function performSearch(keyword) {
            const statusDiv = document.getElementById('searchStatus');
            const resultsDiv = document.getElementById('searchResults');
            
            if (!keyword) {
                statusDiv.innerHTML = '<div class="error">请输入搜索关键词</div>';
                return;
            }
            
            try {
                statusDiv.innerHTML = `<div class="info">正在搜索 "${keyword}"...</div>`;
                console.log(`开始搜索: "${keyword}"`);
                
                const result = await window.api.searchProducts(keyword);
                console.log('搜索结果:', result);
                
                if (result.success) {
                    const products = result.data.products || [];
                    statusDiv.innerHTML = `<div class="success">✓ 搜索成功，找到 ${products.length} 个商品</div>`;
                    
                    // 显示搜索结果
                    if (products.length > 0) {
                        resultsDiv.innerHTML = products.map((product, index) => `
                            <div class="product-item">
                                <h4>${index + 1}. ${product.name}</h4>
                                <p><strong>价格:</strong> ¥${product.price}</p>
                                <p><strong>分类:</strong> ${product.category}</p>
                                <p><strong>ID:</strong> ${product.id}</p>
                                <p><strong>评分:</strong> ${product.average_rating}/5.0 (${product.review_count}条评价)</p>
                                <p><strong>描述:</strong> ${product.description || '无描述'}</p>
                            </div>
                        `).join('');
                    } else {
                        resultsDiv.innerHTML = '<div class="info">没有找到相关商品</div>';
                    }
                } else {
                    statusDiv.innerHTML = `<div class="error">✗ 搜索失败: ${result.error}</div>`;
                    resultsDiv.innerHTML = '';
                }
                
            } catch (error) {
                statusDiv.innerHTML = `<div class="error">✗ 搜索错误: ${error.message}</div>`;
                console.error('搜索错误:', error);
                resultsDiv.innerHTML = '';
            }
        }
        
        // 页面加载完成后运行测试
        window.addEventListener('load', () => {
            console.log('页面加载完成，高级模糊搜索功能测试准备就绪');
            
            // 检查API模块是否加载
            setTimeout(() => {
                if (window.api) {
                    console.log('✓ API模块已加载');
                    console.log('✓ 高级模糊搜索功能已就绪');
                    console.log('✓ 支持连续子串匹配和噪音过滤');
                } else {
                    console.log('✗ API模块未加载');
                }
            }, 100);
        });
    </script>
</body>
</html>
